<?php

require_once(sfConfig::get('sf_plugins_dir').'/sfDoctrineGuardPlugin/modules/sfGuardAuth/lib/BasesfGuardAuthActions.class.php');

class sfGuardAuthActions extends BasesfGuardAuthActions
{
	/**
	 * Surcharge de l'action signin
	 * Ajout tu formulaire de création d'un compte
	 *
	 * @param sfWebRequest $request
	 * @author Vincent
	 * @since 2009-02-21
	 */
	public function executeSignin($request)
	{
		parent::executeSignin($request);
		
		$guard_user = new sfGuardUser();
		$guard_user->setIsActive(0);
		
		$this->guard_user_form = new NewGuardUserForm($guard_user);
	}
	
  public function executeValidateNewGuardUser($request)
  {
    if(!$request->isMethod('post'))
    {
      $this->redirect(url_for($this->getUser()->isAuthenticated() ? '@homepage' : '@sf_guard_signin'));
    }

    $class = sfConfig::get('app_sf_guard_plugin_signin_form', 'sfGuardFormSignin'); 
    $this->form = new $class();
    
    $guard_user = new sfGuardUser();
    $guard_user->setIsActive(0);
    
    $this->guard_user_form = new NewGuardUserForm($guard_user);
    $this->processForm($request, $this->guard_user_form);

    $this->setTemplate('signin');
  }

  protected function processForm($request, $form)
  {
    $form->bind($request->getParameter($form->getName()));
    if ($form->isValid())
    {
      $guard_user = $form->save();
      
      //Envoie des mails
      $this->sendNewUserMails($guard_user);
      
      $this->redirect(url_for('@sf_guard_signin'));
    }
  }
  
  protected function sendNewUserMails($guard_user)
  {
  	$key = wimTools::generateKey();
  	
  	//Set la clef d'activation
  	$guard_user->setValidationKey($key);
  	$guard_user->save();
  	
  	$this->sendUserValidationMail($guard_user);
  	$this->sendAdminRegisterMail($guard_user);
  }
  
  protected function sendUserValidationMail($guard_user)
  {
  	$body = $this->getPartial(
  	  'wimMail/partials/send_register_validation',
  	  array(
  	    'username' => $guard_user->getUsername(),
  	    'key'      => $guard_user->getValidationKey(),
  	  ));
  	
  	$subject = 'Validation de votre inscription';
  	$to = $guard_user->getEmail();
  	$from = sfConfig::get('app_mails_no_reply');
  	  
    try
		{
		  //Create the mailer and message objects
		  $mailer = new Swift(new Swift_Connection_NativeMail());
		  $message = new Swift_Message($subject, $body, 'text/html');
		 
		  //Send
		  $mailer->send($message, $to, new Swift_Address($from, 'Info WhereIsMy'));
		  $mailer->disconnect();
		}
		catch (Exception $e)
		{
		  $mailer->disconnect();
		}
  }
  
  protected function sendAdminRegisterMail($guard_user)
  {
    $body = $this->getPartial(
      'wimMail/partials/send_admin_register_validation',
      array(
        'username' => $guard_user->getUsername(),
        'email'    => $guard_user->getEmail(), 
      ));
    
    $subject = 'Nouvel utilisateur';
    $to = sfConfig::get('app_mails_contact');
    $from = sfConfig::get('app_mails_no_reply');
      
    try
    {
      //Create the mailer and message objects
      $mailer = new Swift(new Swift_Connection_NativeMail());
      $message = new Swift_Message($subject, $body, 'text/html');
     
      //Send
      $mailer->send($message, $to, new Swift_Address($from, 'Info WhereIsMy'));
      $mailer->disconnect();
    }
    catch (Exception $e)
    {
      $mailer->disconnect();
      $this->logMessage($e);
    }
  }
}
